<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\Good;


use App\Http\Controllers\Controller;
use App\Models\Good;
use App\Models\GoodsOneType;
use App\Models\GoodsTwoType;
use App\Models\Order;
use App\Models\UserHome;
use App\Models\UserStatistics;
use App\Models\VipLog;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class GoodTwoTypeController extends Controller
{

    private $namespace = "App\Http\Controllers\Api\V1\Manager\Good\GoodTwoTypeController";

    public function index(Request $request)
    {
        $goods_one_type_id = $request->goods_one_type_id;
        $name = $request->name;
        $pageSize = $request->pageSize;
        $sql = GoodsTwoType::select('t1.*','t2.name as goods_one_type_name')
            ->from((new GoodsTwoType())->getTable().' as t1')
            ->join((new GoodsOneType())->getTable().' as t2','t1.goods_one_type_id','=','t2.id')
            ->where('t1.goods_one_type_id', $goods_one_type_id);
        if (isset($name)) {
            $sql->where('t1.name', 'like', "%$name%");
        }
        $goodsSpecTwoList = $sql->orderBy('t1.weigh', 'asc')
            ->orderBy('t1.id', 'desc')
            ->paginate($pageSize)
            ->toArray();
        $data['goodsSpecTwoList'] = $goodsSpecTwoList['data'];
        $data['totalNum'] = $goodsSpecTwoList['total'];
        return $this->apiResponse($data);
    }

    public function show(Request $request, $id)
    {
        $goodsTwoType = GoodsTwoType::where('id', $id)->first();
        return $this->apiResponse($goodsTwoType);
    }

    public function update(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $goodsTwoType = GoodsTwoType::where('id', $id)->first();
        if ($goodsTwoType['is_yc_switch']) {
            $content = "禁用";
            GoodsTwoType::where('id', $id)->update(['is_yc_switch' => 0]);
        } else {
            $content = "启用";
            GoodsTwoType::where('id', $id)->update(['is_yc_switch' => 1]);
        }
        //添加管理员日志开始
        $url = $this->namespace . "/update";
        $title = "修改";
        $content .= "了id为【".$id."】的商品二级分类";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "商品二级分类模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function destroy(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $goodsNum = Good::where('goods_two_type_id', $id)->where('is_del', 0)->count();
        if ($goodsNum) {
            return $this->apiResponse('', config('errorLiu.YOUWEISHANCHUDELEIMUBUKEJINXING'));
        }
        GoodsTwoType::where('id', $id)->delete();
        //添加管理员日志开始
        $url = $this->namespace . "/destroy";
        $title = "删除";
        $content = "删除了id为【".$id."】的商品二级分类";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "商品二级分类模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->id;
        $data = $request->only('goods_one_type_id', 'name', 'weigh','is_yc_switch','image');
        if (!$data['name'] || empty($data['name']) || !$data['goods_one_type_id']) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $num = GoodsTwoType::where('goods_one_type_id', $data['goods_one_type_id'])->where('id','!=',$id)->where('name',$data['name'])->count();
        if($num){
            return $this->apiResponse('', config('errorLiu.SHUJUMIMGCHENGCHONGFUTOW'));
        }
        if ($id) {
            $content = "修改";
            GoodsTwoType::where('id', $id)->update($data);
        } else {
            $content = "新增";
            $id = GoodsTwoType::insertGetId($data);
        }
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = $content;
        $content .= "了id为【".$id."】的商品二级分类,内容为【".json_encode($data)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "商品二级分类模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }
}